/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author GiangLT
 */
@Entity
@Table(name = "OMAP_USER", catalog = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "OmapUser.findAll", query = "SELECT o FROM OmapUser o"),
    @NamedQuery(name = "OmapUser.findByUserId", query = "SELECT o FROM OmapUser o WHERE o.userId = :userId"),
    @NamedQuery(name = "OmapUser.findByUsername", query = "SELECT o FROM OmapUser o WHERE o.username = :username"),
    @NamedQuery(name = "OmapUser.findByUsernameAndPassword", query = "SELECT o FROM OmapUser o WHERE o.username = :username and o.password = :password"),
    @NamedQuery(name = "OmapUser.findByPassword", query = "SELECT o FROM OmapUser o WHERE o.password = :password"),
    @NamedQuery(name = "OmapUser.findByFullName", query = "SELECT o FROM OmapUser o WHERE o.fullName = :fullName"),
    @NamedQuery(name = "OmapUser.findByUnit", query = "SELECT o FROM OmapUser o WHERE o.unit = :unit"),
    @NamedQuery(name = "OmapUser.findByPasswordFormat", query = "SELECT o FROM OmapUser o WHERE o.passwordFormat = :passwordFormat"),
    @NamedQuery(name = "OmapUser.findByPasswordSalt", query = "SELECT o FROM OmapUser o WHERE o.passwordSalt = :passwordSalt"),
    @NamedQuery(name = "OmapUser.findByEmail", query = "SELECT o FROM OmapUser o WHERE o.email = :email"),
    @NamedQuery(name = "OmapUser.findByMobile", query = "SELECT o FROM OmapUser o WHERE o.mobile = :mobile"),
    @NamedQuery(name = "OmapUser.findByApproved", query = "SELECT o FROM OmapUser o WHERE o.approved = :approved"),
    @NamedQuery(name = "OmapUser.findByLocked", query = "SELECT o FROM OmapUser o WHERE o.locked = :locked"),
    @NamedQuery(name = "OmapUser.findByCreatedDate", query = "SELECT o FROM OmapUser o WHERE o.createdDate = :createdDate"),
    @NamedQuery(name = "OmapUser.findByLastLoginDate", query = "SELECT o FROM OmapUser o WHERE o.lastLoginDate = :lastLoginDate"),
    @NamedQuery(name = "OmapUser.findByLastPwdChangeDate", query = "SELECT o FROM OmapUser o WHERE o.lastPwdChangeDate = :lastPwdChangeDate"),
    @NamedQuery(name = "OmapUser.findByLastLockedDate", query = "SELECT o FROM OmapUser o WHERE o.lastLockedDate = :lastLockedDate"),
    @NamedQuery(name = "OmapUser.findByTimeSlots", query = "SELECT o FROM OmapUser o WHERE o.timeSlots = :timeSlots"),
    @NamedQuery(name = "OmapUser.findByTrustedIps", query = "SELECT o FROM OmapUser o WHERE o.trustedIps = :trustedIps"),
    @NamedQuery(name = "OmapUser.findByComments", query = "SELECT o FROM OmapUser o WHERE o.comments = :comments"),
    @NamedQuery(name = "OmapUser.findByRoleName", query = "SELECT user FROM OmapUser user INNER JOIN user.omapRoleList role WHERE role.roleName = :roleName"),
    @NamedQuery(name = "OmapUser.findBySuperUser", query = "SELECT o FROM OmapUser o WHERE o.superUser = :superUser")})
public class OmapUser implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "USER_ID")
    private Integer userId;
    @Size(max = 100)
    @Column(name = "USERNAME")
    private String username;
    @Size(max = 200)
    @Column(name = "PASSWORD")
    private String password;
    @Size(max = 100)
    @Column(name = "FULL_NAME")
    private String fullName;
    @Size(max = 100)
    @Column(name = "UNIT")
    private String unit;
    @Column(name = "PASSWORD_FORMAT")
    private Short passwordFormat;
    @Size(max = 200)
    @Column(name = "PASSWORD_SALT")
    private String passwordSalt;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 200)
    @Column(name = "EMAIL")
    private String email;
    @Size(max = 200)
    @Column(name = "MOBILE")
    private String mobile;
    @Column(name = "APPROVED")
    private Short approved;
    @Column(name = "LOCKED")
    private Short locked;
    @Column(name = "CREATED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;
    @Column(name = "LAST_LOGIN_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastLoginDate;
    @Column(name = "LAST_PWD_CHANGE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastPwdChangeDate;
    @Column(name = "LAST_LOCKED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastLockedDate;
    @Size(max = 2000)
    @Column(name = "TIME_SLOTS")
    private String timeSlots;
    @Size(max = 2000)
    @Column(name = "TRUSTED_IPS")
    private String trustedIps;
    @Size(max = 2000)
    @Column(name = "COMMENTS")
    private String comments;
    @Column(name = "SUPER_USER")
    private Short superUser;
    @ManyToMany(mappedBy = "omapUserList")
    private List<OmapRole> omapRoleList;
    @JoinTable(name = "USER_PERMISSION", joinColumns = {
        @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "PERMISSION_ID", referencedColumnName = "ID")})
    @ManyToMany
    private List<Permission> permissionList;
//    @OneToMany(mappedBy = "userId")
//    private List<OmapEvent> omapEventList;
    @Transient
    private Boolean bApproved;
    @Transient
    private Boolean bLocked;

    public OmapUser() {
    }

    public OmapUser(Integer userId) {
        this.userId = userId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public Short getPasswordFormat() {
        return passwordFormat;
    }

    public void setPasswordFormat(Short passwordFormat) {
        this.passwordFormat = passwordFormat;
    }

    public String getPasswordSalt() {
        return passwordSalt;
    }

    public void setPasswordSalt(String passwordSalt) {
        this.passwordSalt = passwordSalt;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Short getApproved() {
        return approved;
    }

    public void setApproved(Short approved) {
        this.approved = approved;
    }

    public Short getLocked() {
        return locked;
    }

    public void setLocked(Short locked) {
        this.locked = locked;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Date getLastLoginDate() {
        return lastLoginDate;
    }

    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    public Date getLastPwdChangeDate() {
        return lastPwdChangeDate;
    }

    public void setLastPwdChangeDate(Date lastPwdChangeDate) {
        this.lastPwdChangeDate = lastPwdChangeDate;
    }

    public Date getLastLockedDate() {
        return lastLockedDate;
    }

    public void setLastLockedDate(Date lastLockedDate) {
        this.lastLockedDate = lastLockedDate;
    }

    public String getTimeSlots() {
        return timeSlots;
    }

    public void setTimeSlots(String timeSlots) {
        this.timeSlots = timeSlots;
    }

    public String getTrustedIps() {
        return trustedIps;
    }

    public void setTrustedIps(String trustedIps) {
        this.trustedIps = trustedIps;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Short getSuperUser() {
        return superUser;
    }

    public void setSuperUser(Short superUser) {
        this.superUser = superUser;
    }

    public Boolean getBApproved() {
        bApproved = (approved != null && approved == 1) ? true : false;
        return bApproved;
    }

    public void setBApproved(Boolean bApproved) {
        approved = bApproved ? (short) 1 : 0;
        this.bApproved = bApproved;
    }

    public Boolean getBLocked() {
        bLocked = (locked != null && locked == 1) ? true : false;
        return bLocked;
    }

    public void setBLocked(Boolean bLocked) {
        locked = bLocked ? (short) 1 : 0;
        this.bLocked = bLocked;
    }

    @XmlTransient
    public List<OmapRole> getOmapRoleList() {
        return omapRoleList;
    }

    public void setOmapRoleList(List<OmapRole> omapRoleList) {
        this.omapRoleList = omapRoleList;
    }

//    @XmlTransient
//    public List<OmapEvent> getOmapEventList() {
//        return omapEventList;
//    }
//
//    public void setOmapEventList(List<OmapEvent> omapEventList) {
//        this.omapEventList = omapEventList;
//    }

    @XmlTransient
    public List<Permission> getPermissionList() {
        return permissionList;
    }

    public void setPermissionList(List<Permission> permissionList) {
        this.permissionList = permissionList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OmapUser)) {
            return false;
        }
        OmapUser other = (OmapUser) object;
        if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.thienloc.omap.entity.OmapUser[ userId=" + userId + " ]";
    }
}
